package com.shadow.domain;

import org.hibernate.annotations.Cascade;

import javax.persistence.*;
import java.beans.Transient;
import java.io.UnsupportedEncodingException;
import java.sql.Blob;
import java.sql.SQLException;

/**
 * Created by qq65827 on 2015/1/26.
 */
@Entity
@Table(name="issue_resource")
public class IssueResource extends BaseObject {


    @Transient
    public Boolean cover(IssueSnapshot issueSnapshot) {
        this.setDelete(issueSnapshot.isDelete());
        this.setContent(issueSnapshot.getContent());
        this.setDownloads(issueSnapshot.getDownloads());
        this.setName(issueSnapshot.getName());
        this.setPath(issueSnapshot.getPath());
        this.setText(issueSnapshot.getText());
        this.setType(issueSnapshot.getType());
        this.setIssue(issueSnapshot.getIssue());
        this.setSize(issueSnapshot.getSize());
        return true;
    }

    @Transient
    public String getHtmlText() throws SQLException, UnsupportedEncodingException {
        byte[] bytes =  getContent().getBytes(1, (int) getContent().length());
        String text = new String(bytes, "utf-8");
        return text;
    }

    @ManyToOne
    @JoinColumn(name = "f_issue_id" )
    @Cascade(value = org.hibernate.annotations.CascadeType.ALL)
    private Issue issue;


    @Enumerated(EnumType.ORDINAL)
    @Column(name = "f_type")
    private ResourceType type;

    @Column(name = "f_name")
    private String name;

    @Column(name = "f_text")
    private String text;

    @Column(name = "f_size")
    private Long size;

    public Long getSize() {
        return size;
    }

    public void setSize(Long size) {
        this.size = size;
    }

    @Column(name = "f_path")
    private String path;

    @Column(name = "f_content")
    private Blob content;


    public ResourceType getType() {
        return type;
    }

    public void setType(ResourceType type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public Blob getContent() {
        return content;
    }

    public void setContent(Blob content) {
        this.content = content;
    }

    public Integer getDownloads() {
        return downloads;
    }

    public void setDownloads(Integer downloads) {
        this.downloads = downloads;
    }

    @Column(name = "f_downloads")
    private Integer downloads;

    public Issue getIssue() {
        return issue;
    }

    public void setIssue(Issue issue) {
        this.issue = issue;
    }

    public static enum ResourceType{
        html,css,javascript, image,
    }

    public IssueResource() {

    }

    public IssueResource(ResourceType type, Blob content) {
        this.type = type;
        this.content = content;
    }



}